/* Delete ViewModel */

var deleteConfirmViewModel = function() {

	var self = this;
	self.currentItem = ko.observable("");
	self.parentItem = ko.observable("");
	self.type = ko.observable("");
	self.confirmationText = ko.observable("Do you want to delete?");

	self.deleteItem = function() {
		console.log("-------->Deleting Item:: ", self.type());
		console.log("currentItem:", self.currentItem())
		console.log("parentItem:", self.parentItem())

		self.confirmationText("Do you want to delete?");

		if (self.type() == "grade") {
			if (self.parentItem().sectionId() != "") {
				jQuery.ajax({
					data : {
						sectionId : self.parentItem().sectionId(),
						ceGradeId : self.currentItem().ceGradeId()
					},
					url : url + "deleteSectionGrade",
					type : "post",
					success : function(result) {
						console.log(result);
						if (result == "true") {
							var index = self.parentItem().grades().indexOf(self.currentItem());
							self.parentItem().grades.splice(index, 1);

							for (var k = 0; k < filterGradeList.length; k++) {
								if (filterGradeList[k] == self.currentItem().ceGradeId()) {
									filterGradeList.splice(k, 1);
									break;
								}
							}
							
							ko.dataFor(document.getElementById('create-Section')).OriginalgradesList(grades);
						}

					},
					error : function(jqXHR, textStatus, errorThrown) {
						console.log("error");
					}
				});
			} else {
				var index = self.parentItem().grades().indexOf(self.currentItem());
				self.parentItem().grades.splice(index, 1);
			}

		} else if (self.type() == "section") {
			if (self.currentItem().sectionId() != "") {
				jQuery.ajax({
					data : {
						sectionId : self.currentItem().sectionId()
					},
					url : url + "deleteAcademicSection",
					type : "post",
					success : function(result) {
						console.log(result);
						if (result == "true") {

							// Will delete category list
							for (var i = 0; i < sectionCategories.length; i++) {
								if (sectionCategories[i].sectionId == self.currentItem().sectionId()) {
									console.log("deleting category");
									sectionCategories.splice(i, 1);
									ko.dataFor(document.getElementById("add-Event")).availableCategories(sectionCategories);
									break;
								}
							}

							var viewModel = ko.dataFor(document.getElementById("tabs-3"));

							for (var i = 0; i < viewModel.events().length; i++) {
								if (viewModel.events()[i].eventSectionId() == self.currentItem().sectionId()) {
									console.log("--------> deleting event of deleted section");
									viewModel.events.splice(i, 1);
								}
							}

							var index = self.parentItem().sections().indexOf(self.currentItem());
							self.parentItem().sections.splice(index, 1);

							for (var l = 0; l < self.currentItem().grades().length; l++) {
								for (var k = 0; k < filterGradeList.length; k++) {
									if (filterGradeList[k] == self.currentItem().grades()[l].ceGradeId()) {
										filterGradeList.splice(k, 1);
										break;
									}
								}
							}
							
							ko.dataFor(document.getElementById('create-Section')).OriginalgradesList(grades);
						}

					},
					error : function(jqXHR, textStatus, errorThrown) {
						console.log("error");
					}
				});
			} else {
				var index = self.parentItem().sections().indexOf(self.currentItem());
				self.parentItem().sections.splice(index, 1);
			}

		} else if (self.type() == "holiday") {
			if (self.currentItem().eventId() != "") {
				jQuery.ajax({
					data : {
						eventId : self.currentItem().eventId(),
						eventSectionId : ""
					},
					url : url + "deleteSchoolEvent",
					type : "post",
					success : function(result) {
						console.log(result);
						if (result == "true") {
							var index = self.parentItem().holidays().indexOf(self.currentItem());
							self.parentItem().holidays.splice(index, 1);
						}
						
						getWorkingDays();
						updateAllDates();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						console.log("error");
					}
				});
			} else {
				var index = self.parentItem().holidays().indexOf(self.currentItem());
				self.parentItem().holidays.splice(index, 1);
			}

		} else if (self.type() == "event") {
			if (self.currentItem().eventId() != "") {
				jQuery.ajax({
					data : {
						eventId : self.currentItem().eventId(),
						eventSectionId : self.currentItem().eventSectionId()
					},
					url : url + "deleteSchoolEvent",
					type : "post",
					success : function(result) {
						console.log(result);
						if (result == "true") {
							var index = self.parentItem().events().indexOf(self.currentItem());
							self.parentItem().events.splice(index, 1);
							myAcademicYearModel.setEventData(self.parentItem().events());
						}
						
						getWorkingDays();
						updateAllDates();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						console.log("error");
					}
				});
			} else {
				var index = self.parentItem().events().indexOf(self.currentItem());
				self.parentItem().events.splice(index, 1);
			}

		}

	}

	self.cancelItem = function() {
		console.log("------> cancel delete item");
		self.confirmationText("Do you want to delete?");
	}
}